package pers.rcklos.myserver.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentLinkedQueue;

public class DaoUtils {
	
	public DaoUtils() {}		// 屏蔽构造方法
	
	// 静态池
//	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static final String MY_URL = "jdbc:mysql://localhost:3306/school_paper?characterEncoding=utf-8&serverTimezone=UTC";
	private static final String MY_USER = "root";
	private static final String MY_PASSWORD = "zhangchuyu...";
	
	private static ConcurrentLinkedQueue<Connection> connPool = new ConcurrentLinkedQueue<>();
	
	// 常量池
	// 查询
	public static final int METHOD_QUERY  = 0;
	// 更新
	public static final int METHOD_UPDATE = 1;
	
	static {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			for(int i = 0; i < 5; i++)
				connPool.add(DriverManager.getConnection(MY_URL, MY_USER, MY_PASSWORD));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	private static Connection getConn() {
		if(connPool.isEmpty())
			try {
				return DriverManager.getConnection(MY_URL, "root", "zhangchuyu...");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else return connPool.poll();
		return null;
	}
	
	// 公用方法
//	private static String formatDate(Date date) {
//		if(date == null) return null;
//		return sdf.format(date);
//	}
	
	private static void closeConn(Connection conn) {
		connPool.add(conn);
	}
	
	private static void closeAll(PreparedStatement ps, ResultSet rs) throws SQLException {
		if(ps != null) ps.close();
		if(rs != null) rs.close();
	}
	
	private static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
		closeConn(conn);
		try {
			closeAll(ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
//	private static int exeUpdate(PreparedStatement ps) {
//		int rs = -1;
//		if(ps == null) return rs;
//		try {
//			rs = ps.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				closeAll(ps, null);
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		return rs;
//	}
	
	private static DaoConnection execQuery(Connection conn, PreparedStatement ps) throws SQLException {
		ResultSet rs = ps.executeQuery();
		DaoConnection dc = DaoConnection.newDaoConnectionBuilder()
				.setConn(conn)
				.setPs(ps)
				.setRs(rs)
				.build();
		return dc;
	}
	
	private static DaoConnection execUpdate(Connection conn, PreparedStatement ps) throws SQLException {
		int rs = ps.executeUpdate();
		DaoConnection dc = DaoConnection.newDaoConnectionBuilder()
				.setConn(conn)
				.setPs(ps)
				.setRsInt(rs)
				.build();
		return dc;
	}
	
	public static DaoConnection fetchDaoConnection(String sql, Object[] objects, int method) throws SQLException {
		Connection conn = getConn();
		PreparedStatement ps = conn.prepareStatement(sql);
		if(objects != null)
			for(int i = 0; i < objects.length; i++) {
				ps.setObject(i + 1, objects[i]);
			}
		System.out.println("DaoUtils: fetchDaoConnection");
		switch(method) {
		case METHOD_QUERY:
			return execQuery(conn, ps);
		case METHOD_UPDATE:
			return execUpdate(conn, ps);
		}
		return null;
	}
	
	public static void closeAll(DaoConnection dc) {
		if(dc == null) return;
		closeAll(dc.getConn(), dc.getPs(), dc.getRs());
	}
	
}
